/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_fastdds/listener_impl/SubscriberListenerImpl.hpp"
#include "dal_fastdds/entity_impl/DalSubscriberImpl.hpp"
#include "dal_fastdds/entity_impl/DalDataReaderImpl.hpp"
#include "dal_fastdds/utils/log.hpp"
#include "dal_fastdds/utils/EntityMappingManager.hpp"

SubscriberListenerImpl::SubscriberListenerImpl(dal_subscriber_listener_callbacks_t callbacks)
    : callbacks_(callbacks)
{
}

SubscriberListenerImpl::~SubscriberListenerImpl() {}

void SubscriberListenerImpl::on_data_on_readers(Subscriber* sub)
{
    if (callbacks_.on_data_on_readers == nullptr)
        return;

    dal_subscriber_t* dal_sub = EntityMappingGetSubscriber(sub);
    if (!dal_sub) {
        return;
    }

    void* data = callbacks_.data_;
    (callbacks_.on_data_on_readers)(dal_sub, data);
}

void SubscriberListenerImpl::on_sample_lost(DataReader* reader, const SampleLostStatus& status)
{
    if (callbacks_.on_sample_lost == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_sample_lost_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSampleLostStatusFromFastddsToDal(&status, &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_sample_lost)(dal_reader, &dal_status, data);
}

void SubscriberListenerImpl::on_data_available(DataReader* reader)
{
    if (callbacks_.on_data_available == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    void* data = callbacks_.data_;
    (callbacks_.on_data_available)(dal_reader, data);
}

void SubscriberListenerImpl::on_sample_rejected(DataReader* reader,
                                                const SampleRejectedStatus& status)
{
    if (callbacks_.on_sample_rejected == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_sample_rejected_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSampleRejectedStatusFromFastddsToDal(&status,
                                                                                &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_sample_rejected)(dal_reader, &dal_status, data);
}

void SubscriberListenerImpl::on_liveliness_changed(DataReader* reader,
                                                   const LivelinessChangedStatus& status)
{
    if (callbacks_.on_liveliness_changed == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_liveliness_changed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertLivelinessChangedStatusFromFastddsToDal(&status,
                                                                                   &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_liveliness_changed)(dal_reader, &dal_status, data);
}

void SubscriberListenerImpl::on_requested_deadline_missed(DataReader* reader,
                                                          const DeadlineMissedStatus& status)
{
    if (callbacks_.on_requested_deadline_missed == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_requested_deadline_missed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertRequestedDeadlineMissedStatusFromFastddsToDal(
        &status, &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_requested_deadline_missed)(dal_reader, &dal_status, data);
}

void SubscriberListenerImpl::on_requested_incompatible_qos(
    DataReader* reader, const RequestedIncompatibleQosStatus& status)
{
    if (callbacks_.on_requested_incompatible_qos == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_requested_incompatible_qos_status_t dal_status;
    StatusConverter::GetInstance()->ConvertRequestedIncompatibleQosStatusFromFastddsToDal(
        &status, &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_requested_incompatible_qos)(dal_reader, &dal_status, data);
}

void SubscriberListenerImpl::on_subscription_matched(DataReader* reader,
                                                     const SubscriptionMatchedStatus& info)
{
    if (callbacks_.on_subscription_matched == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_subscription_matched_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSubscriptionMatchedStatusFromFastddsToDal(&info,
                                                                                     &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_subscription_matched)(dal_reader, &dal_status, data);
}